package com.dm.wallpaper.library.utils;


import com.dm.wallpaper.library.ResourceTable;
import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;


public class Tooltip {

    private final PopupDialog mPopupWindow;
    private final Builder mBuilder;
    private boolean mCheckboxState = false;

    private Tooltip(Builder builder) {
        mBuilder = builder;
        Component pop_itemlayout = LayoutScatter.getInstance(builder.mContext).parse(ResourceTable.Layout_tool_tip_layout, null, true);

        Text tv_des = (Text) pop_itemlayout.findComponentById(ResourceTable.Id_tv_des);
        tv_des.setText(builder.mDesc);

        Text tv_content = (Text) pop_itemlayout.findComponentById(ResourceTable.Id_tv_content);
        tv_content.setText(builder.mContent);

        Image iocn = (Image) pop_itemlayout.findComponentById(ResourceTable.Id_iv_des);
        VectorElement vectorElement = new VectorElement(builder.mContext, builder.mDescIcon);

        iocn.setImageElement(vectorElement);

        pop_itemlayout.findComponentById(ResourceTable.Id_btn_ok).setClickedListener(component -> {
            if (mBuilder.mCallback != null) {
                mBuilder.mCallback.onButtonClick(this);
            }
        });
        Checkbox checkbox = (Checkbox) pop_itemlayout.findComponentById(ResourceTable.Id_checkbox);
        checkbox.setCheckedStateChangedListener((absButton, b) -> {
            mCheckboxState = b;
        });
        mPopupWindow = new PopupDialog(mBuilder.mContext, pop_itemlayout);
        //   mPopupWindow.setSize((int) (SystemUtils.getDisplayWidthInPx(mBuilder.mContext) * 0.7), (int) SystemUtils.fp2px(mBuilder.mContext, 200));
        mPopupWindow.setSize((int) (SystemUtils.getDisplayWidthInPx(mBuilder.mContext) * 0.7), ComponentContainer.LayoutConfig.MATCH_CONTENT);
        mPopupWindow.setCustomComponent(pop_itemlayout);
        mPopupWindow.setBackColor(Color.TRANSPARENT);
        mPopupWindow.setAutoClosable(mBuilder.mCancelable);

    }

    public boolean getCheckboxState() {
        return mCheckboxState;
    }

    public void show() {
        Component component = mBuilder.mTo;
        int[] local = component.getLocationOnScreen();
        mPopupWindow.showOnCertainPosition(LayoutAlignment.LEFT | LayoutAlignment.TOP, local[0], local[1] - 600);
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.destroy();
        }
    }


    public static Builder Builder(Context context) {
        return new Builder(context);
    }

    public static class Builder {

        private final Context mContext;
        private Component mTo;
        private String mContent;
        private String mDesc;
        private int mDescIcon;
        private Callback mCallback;
        private boolean mCancelable;

        private Builder(Context context) {
            mContext = context;
            mContent = "";
            mDescIcon = 0;
            mCancelable = true;
        }

        public Builder to(Component to) {
            mTo = to;
            return this;
        }

        public Builder cancelable(boolean cancelable) {
            mCancelable = cancelable;
            return this;
        }

        public Builder content(int res) {
            try {
                mContent = mContext.getResourceManager().getElement(res).getString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return this;
        }

        public Builder desc(int res) {
            try {
                mDesc = mContext.getResourceManager().getElement(res).getString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return this;
        }

        public Builder descIcon(int res) {
            mDescIcon = res;
            return this;
        }



        public Builder buttonCallback(Callback callback) {
            mCallback = callback;
            return this;
        }

        public Builder visibleDontShowAgain(boolean show) {
            return this;
        }

        public Tooltip build() {
            return new Tooltip(this);
        }

        public void show() {
            build().show();
        }
    }


    public interface Callback {
        void onButtonClick(Tooltip tooltip);
    }
}
